package org.example.all;

import com.esotericsoftware.reflectasm.MethodAccess;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Yyming
 * @version 10.0
 * Created by Yyming on 2021/2/25
 */
public class ReflectTest {

    public static void main(String[] args) throws Exception {
        Method method = Object.class.getMethod("hashCode");
        Method method22 = Object.class.getMethod("hashCode");
        Map<String, Method> map = new HashMap<>();
        map.put("qqq",null);
        Method qqq = map.getOrDefault("qqq", method);
        testObjectMethod();
    }

    private static void testObjectMethod() throws Exception{
        Method method = Object.class.getDeclaredMethod("clone");
        method.setAccessible(true);

        App app = new App();
        App app1 = new App();

        Object invoke = method.invoke(app);
        Object invoke1 = method.invoke(app1);

        int a = 1;


    }

    private void testAsm() throws Exception{
        Object o = new App();
        int t = 100000000;
        long l = System.currentTimeMillis();
        Method clipMethod = o.getClass().getDeclaredMethod("clone", new Class[0]);
        clipMethod.setAccessible(true);
        for (int i = 0; i < t; i++) {
            Object invoke = clipMethod.invoke(o);
        }
        System.out.println(System.currentTimeMillis() - l);
        l = System.currentTimeMillis();

        MethodAccess access = MethodAccess.get(o.getClass());
        int clone1 = access.getIndex("clone");
        for (int i = 0; i < t; i++) {
            Object clone = access.invoke(o, clone1);
        }

        System.out.println(System.currentTimeMillis() - l);
    }
}
